package com.qdlc.p2p.dal.dao.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.stereotype.Repository;

import com.qdlc.p2p.common.util.BeanUtil;
import com.qdlc.p2p.common.util.DateUtil;
import com.qdlc.p2p.common.util.StringUtil;
import com.qdlc.p2p.dal.common.dao.jpa.BaseDaoImpl;
import com.qdlc.p2p.dal.common.model.jpa.OrderFilter;
import com.qdlc.p2p.dal.common.model.jpa.PageDataList;
import com.qdlc.p2p.dal.common.model.jpa.QueryParam;
import com.qdlc.p2p.dal.common.model.jpa.SearchFilter;
import com.qdlc.p2p.dal.dao.LotteryRecordDao;
import com.qdlc.p2p.dal.dao.UserDao;
import com.qdlc.p2p.dal.dto.LotteryRecord;
import com.qdlc.p2p.dal.dto.User;
import com.qdlc.p2p.dal.model.LotteryRecordModel;

@Repository("lotteryRecordDao")
public class LotteryRecordDaoImpl extends BaseDaoImpl<LotteryRecord> implements LotteryRecordDao {

    @Override
    public List<LotteryRecordModel> getNewList(int category) {
        QueryParam param = QueryParam.getInstance();
        param.addOrder(OrderFilter.OrderType.DESC, "id");
        List<LotteryRecord> list = this.findByCriteria(param, 0, LotteryRecordModel.SHOW_RECORD_COUNT);
        List<LotteryRecordModel> modelList = new ArrayList<LotteryRecordModel>();
        if (list != null && list.size() > 0) {
            for (LotteryRecord entity : list) {
                LotteryRecordModel model = new LotteryRecordModel();
                UserDao userDao = (UserDao) BeanUtil.getBean("userDao");
                User user = userDao.find(entity.getUserId());
                if (null == user) continue;
                model.setUserName(user.getHideUserName());
                model.setType(entity.getType());
                model.setValue(entity.getValue());
                modelList.add(model);
            }
        }
        return modelList;
    }

    @Override
    public PageDataList<LotteryRecordModel> list(LotteryRecordModel model) {
        QueryParam param = QueryParam.getInstance().addPage(model.getPage(), model.getRows());
        if(!StringUtil.isBlank(model.getSearchName())){//模糊查询条件
            param.addParam("name", SearchFilter.Operators.LIKE , model.getSearchName());
        }
        if (StringUtil.isNotBlank(model.getName())) {
            param.addParam("name", SearchFilter.Operators.LIKE , model.getName());
        }
        if (StringUtil.isNotBlank(model.getStartTime())) {
            Date start = DateUtil.valueOf(model.getStartTime() + " 00:00:00");
            param.addParam("addTime", SearchFilter.Operators.GTE, start);
        }
        if (StringUtil.isNotBlank(model.getEndTime())) {
            Date end = DateUtil.valueOf(model.getEndTime() + " 23:59:59");
            param.addParam("addTime", SearchFilter.Operators.LTE, end);
        }
        if (model.getType() != 99) {
            param.addParam("type", model.getType());
        }
        PageDataList<LotteryRecord> list = this.findPageList(param);
        PageDataList<LotteryRecordModel> modelDataList = new PageDataList<LotteryRecordModel>();
        List<LotteryRecordModel> modelList = new ArrayList<LotteryRecordModel>();
        modelDataList.setPage(list.getPage());
        if (list.getList().size() > 0) {
            for (LotteryRecord entity : list.getList()) {
                LotteryRecordModel lotteryRecordModel = LotteryRecordModel.instance(entity);
                UserDao userDao = (UserDao) BeanUtil.getBean("userDao");
                User user = userDao.find(entity.getUserId());
                if (null != user)
                    lotteryRecordModel.setUserName(user.getUserName());
                modelList.add(lotteryRecordModel);
            }
        }

        modelDataList.setList(modelList);
        return modelDataList;
    }
}
